/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.gui.impl;

import com.rh.game.chat.ChatImage;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import java.io.InputStream;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Hayden
 */
public class ImageJPanel extends JPanel {
    // com.rh.gui.impl.ImageJPanel
    public static void main(String[] args)  {
        System.out.println("ImageJPanel");
        ChatImage ci = ChatImage.NULL;
        ImageJPanel ijp = new ImageJPanel(com.rh.game.impl.Jayden1.JAYDEN_MALE);
        JOptionPane.showConfirmDialog(null, ijp);
    }

    private Image img = null;
    private ChatImage image = null;
    private boolean render = false;
    
    ImageJPanel(ChatImage pImage)  {
        this(pImage, pImage != ChatImage.NULL);
    }
    
    private ImageJPanel(ChatImage pImage, boolean pVisible)  {
        super();
        image = pImage;
        render = pVisible;
        initComponents();
    }
    
    public void paint(Graphics g) {
        if (render)  {
            g.drawImage(img, 0, 0, null);
        }
        else  {
            super.paint(g);
        }
    }

    private static void debug(Object o)  {
        System.out.println("ImageJPanel: " + o);
    }
    
    public static void close(InputStream is)  {
        if (is == null)  return;
        try  {
            is.close();
        }
        catch (java.io.IOException ioe)  {
            ioe.printStackTrace();
        }
    }
    
    private void initComponents()  {
        debug("image <= " + image);
        setVisible(false);
        if (render)  {
            InputStream is = null;
            String x = image.getValue();
            debug("Loading image := " + x);
            try  {
                is = this.getClass().getResourceAsStream(x);
                debug("InputStream := " + is);
                
                if (is == null)  {
                    x = "/" + x;
                    is = this.getClass().getResourceAsStream(x);
                    debug("InputStream2 := " + is);                    
                }
                
                int available = is.available();
                byte[] b = new byte[available];
                while (available > 0)  {
                    int count = 0;
                    while (count < available)  {
                        count += is.read(b, count, available - count);
                    }
                    
                    available = is.available();
                    if (available > 0)  {
                        byte[] temp = new byte[b.length + available];
                        System.arraycopy(b, b.length, temp, 0, b.length);
                        b = temp;
                    }
                }
                
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                MediaTracker tracker = new MediaTracker(this);
                
                img = toolkit.createImage(b);
                tracker.addImage(img, 0);
                tracker.waitForAll();
            }
            catch (IOException ioe)  {
                ioe.printStackTrace();
            }
            catch (InterruptedException ie)  {
                ie.printStackTrace();
            }
            finally  {
                close(is);
            }

            int w = Math.max(img.getWidth(null), 50);
            int h = Math.max(img.getHeight(null), 50);
            this.setPreferredSize(new Dimension(w, h));
            
            setVisible(true);
        }
    }
}
